<?php

/**
 * @Author:      lyt
 * @DateTime:    2015-10-08 16:36:20
 * @Description: 用于维护 耗材产品项
 */

class SupplyProductItem extends Eloquent
{

    protected $table = "supply_product_item";



    public static $status = array(
        "1"=>"开启",
        "0"=>"关闭",
    );

    public static $is_right = array(
        "1"=>"适用",
        "0"=>"不适用",
    );

    public function price()
    {
        return $this->hasOne('SupplyProductPrice',"supply_product_id","id");
    }

     public function item()
    {
        return $this->belongsTo('SupplyItem',"supply_item_id","id");
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-01-19 15:15:01
     * @Description: 用于获取指定车型指定 耗材品类的 方法
     */
    public static function getDefault($car_model_id,$supply_subject){
        $arr= DB::select("SELECT spi.number,spi.max_number,si.*,ss.`name`,sb.brand_name,ssb.name as ssb_name FROM `supply_product_item` spi
            LEFT JOIN supply_item si on si.id=spi.supply_item_id
            LEFT JOIN supply_brand sb on si.brand_id=sb.id
            LEFT JOIN supply_series ss on ss.id=si.series_id
            LEFT JOIN supply_subject ssb on ssb.id=si.`subject`
            where spi.`status`=1 and si.`status`=1 and ss.`status`=1 and sb.`status`=1 and ssb.`status`=1
            and spi.car_model_id=? and `subject`=? order by spi.isdefualt desc ",array($car_model_id,$supply_subject));
        return isset($arr[0])?$arr[0]:null;
    }

    /**
     * @Author:dx
     * @DateTime:2017-04-19 14:09:41
     * @Description: 根据车型 匹配合适的产品包
     */
    public static function getRightSupplyProductItem($car_model_id,$subject_id){
        $spi=DB::select("SELECT si.id,si.supply_classification_id as subject
                FROM anhui_data.supply_product_item spi
                LEFT JOIN supply_item si on spi.supply_item_id=si.id
                where spi.car_model_id=? and spi.status=1 and si.status=1 and si.supply_classification_id=?",array($car_model_id,$subject_id));
        return $spi;
    }
}
